package model

import (
	"encoding/json"
	"fmt"
	"io/fs"
	"io/ioutil"
	"os"
	"testing"
)

const (
	buildDir string = "./target/generated-sources"
)

// TestSchema generates json schema file
func TestSchema(t *testing.T) {
	schema := Schema()

	actual, err := json.MarshalIndent(schema, "", "  ")
	if err != nil {
		t.Fatalf("failed to generate schema: %s", err)
	}

	if err = os.MkdirAll(buildDir, fs.ModePerm); err != nil {
		t.Fatalf("failed to create dir '%s': %s", buildDir, err)
	}

	err = ioutil.WriteFile(fmt.Sprintf("%s/schema.json", buildDir), actual, fs.ModePerm)
	if err != nil {
		t.Fatalf("failed to write file: %s", err)
	}
}
